using Client.Base;
using System;
using UnityEngine;

namespace Client.Hotfix
{
    public abstract class CUI : IAwake, IDestroy
    {
        protected PrefabAttribute attribute;
        public GameObject Entity { get; protected set; }
        public bool HasEntity=>Entity != null;
        protected ReferenceCollector rc;
        public string DefPrefab => attribute.Prefab;
        protected object arg;
        public bool IsShow { get; private set; }
        public CUI()
        {
            attribute = (PrefabAttribute)Attribute.GetCustomAttribute(GetType(), typeof(PrefabAttribute));
        }

        public void Awake(params object[] args)
        {
            Entity = (GameObject)args[0];
            rc = Entity.GetComponent<ReferenceCollector>();
            Initializate();
        }

        public void Show(object arg = null)
        {
            if (IsShow) return;

            this.arg = arg;
            OnShow();
            var parent = UIRoot.Instance.GetLayer(attribute.Layer);
            Entity.transform.SetParent(parent, false);
            if (attribute.SubIndex != -1)
            {
                Entity.transform.SetSiblingIndex(attribute.SubIndex);
            }
            Entity.SetActive(true);
            IsShow = true;
        }

        public void Hide()
        {
            Entity.gameObject.SetActive(false);
            OnHide();
            IsShow = false;
        }
        public void Destroy()
        {
            IsShow = false;
            OnDestroy();
            GameObject.Destroy(Entity);
        }

        protected virtual void Initializate() { }
        protected virtual void OnShow() { }
        protected virtual void OnHide() { }
        protected virtual void OnDestroy() { }

    }

}


